package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.entity.Publisher;
import com.wanmait.employment.entity.ResumViewsHistory;
import com.wanmait.employment.entity.Userinfo;
import com.wanmait.employment.mapper.PublisherMapper;
import com.wanmait.employment.mapper.ResumViewsHistoryMapper;
import com.wanmait.employment.mapper.UserinfoMapper;
import com.wanmait.employment.service.ResumViewHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ResumViewHistoryImpl implements ResumViewHistoryService {

    @Resource
    private ResumViewsHistoryMapper resumViewsHistoryMapper;
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private PublisherMapper publisherMapper;

    @Override
    public PageInfo<ResumViewsHistory> selectList(String title,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);


        QueryWrapper<ResumViewsHistory> resumViewsHistoryQueryWrapper = new QueryWrapper<>();
        try{
            Integer i=Integer.parseInt(title);
            resumViewsHistoryQueryWrapper.like("count",title);
        }catch (Exception e){
            resumViewsHistoryQueryWrapper=null;
        }

        List<ResumViewsHistory> resumViewsHistoryList = resumViewsHistoryMapper.selectList(resumViewsHistoryQueryWrapper);
        PageInfo<ResumViewsHistory> resumViewsHistoryPageInfo = new PageInfo<>(resumViewsHistoryList);
        resumViewsHistoryPageInfo.getList().forEach(resumViewsHistory1 -> {
            QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
            userinfoQueryWrapper.eq("id",resumViewsHistory1.getUserinfoId());
            Userinfo userinfo = userinfoMapper.selectOne(userinfoQueryWrapper);
            resumViewsHistory1.setUserinfo(userinfo);

            QueryWrapper<Publisher> publisherQueryWrapper=new QueryWrapper<>();
            publisherQueryWrapper.eq("id",resumViewsHistory1.getPublisherId()).or().like("name",title);
            Publisher publisher=publisherMapper.selectOne(publisherQueryWrapper);
            resumViewsHistory1.setPublisher(publisher);

        });
        return resumViewsHistoryPageInfo;
    }

    @Override
    public void delete(Integer id) {

      resumViewsHistoryMapper.deleteById(id);

    }
}
